<?php
/*
 * Created on Sep 7, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 function CreateBoxView($result, $linkArray = 0, $editCell = false) {

	//All before next comment is model stuff concider transfering to getRow, getCols
	
	$boxQuery = "SELECT numRows, numCols, LetterHeight, LetterWidth, Tank, Tower, Position " .
			"FROM ContainerType JOIN Container ON (Container.ContainerTypeID=ContainerType.id) " .
			"WHERE Container.ID=" . mysql_real_escape_string($_GET['ContainerID']);
	$box = mysql_query($boxQuery) or die('Query failed in Create Box View: ' . mysql_error() . "Query : $query");  //gets box dementions
	$databaseRow = mysql_fetch_array($box, MYSQL_ASSOC);
	$numRow = $databaseRow['numRows'];
	$numCol = $databaseRow['numCols']; 
	//Nesting tables to display info in a list format 
	$select .= $databaseRow['Tank'] . " " . $databaseRow['Tower'] . " " . $databaseRow['Position'];
	
	$select .= "<table border = 0> \n";
	//$select .= "	<tr> \n";
	//$select .= "		<td></td> \n";
	//$select .= "	</tr> \n";
	for ($col = 0; $col < $numCol; $col++) { //
		$select .= "	<tr> \n";
		/*
		$header = $col + 1;
		if($databaseRow['LetterHeight'])
			//$header = ModifyFrozenTube::convertToLetter($header);
		$select .= "<th>$header</th>";
  			*/
		for($row = 0; $row < $numRow; $row++) {
			$line = mysql_fetch_array($result, MYSQL_ASSOC);
			//echo "$Line: $line Row: $row Col: $col";
			$frozenTubeID = $line['id'];
			$bgColor = "bgcolor = \"";
			if (checkFrozenTube($frozenTubeID)) { //Hightlight
				$bgColor .= "FFFFCC";
			} elseif(($col % 2 == 0 & $row % 2 ==0) | ($col % 2 == 1 & $row % 2 == 1)) {
				$bgColor .= "EEEEFF";
			} else {
				$bgColor .= "EEEEEE"	;
			}
			$bgColor .= "\"";
  			$select .= "		<td Valign=\"top\" $bgColor> \n";
  				
			
			//$select .= $line['Name'];
			$select .="					<table width=\"100%\">";
			$select .=makeCell($line, $linkArray, True, $editCell, $frozenTubeID);
			$select .="					</table> \n";
			$select .="			</td> \n";
  					
  			}
  		$select .= "		</tr> \n";
	}
	$select .= "</table> \n";
	mysql_free_result($result); //should not be here

	return $select;
}	

/*
 * Returns a string of all information 
 * $line is the information to the added to the database 
 * $linkArray is an array of values tied to links to other pages
 * $formatCell returns the information table formatted instead 
 * instead of a string.
 */
 function makeCell($line, $linkArray, $formatCell = False, $editCell = False, $frozenTubeID) {
 	
			
 
	$loopCount = 0;
	if($formatCell) {
		//This is the color of the top of the box 
		if(isset($_GET['id']) && $_GET['id'] == $frozenTubeID) {
			$bgColor = "FFFFFF";
		}
		elseif (checkFrozenTube($frozenTubeID)) { //Highlight
			$bgColor = 'FFFF00';
		} else {
			$bgColor = 'FFCC66';
		}
		$select .= "<tr valign=\"top\" bgcolor=\"$bgColor\"> \n";
		$select .= "	<td> ";
		//This is the top part of the box.
		$search = makeSearchLink($_GET);
		//Only Student Helpers and Admins can edit boxes in any way shape or form.
		//if(isset($_COOKIE["Helper"])) {
			$select .=		"<font size=\"1.5\" color= \"3399CC\" > <a href=\"ModifyTube.php?id=" . $line['id'] . "&$search\">" . printLocation($line) . "</a> </font>  \n";  
		/*
		} else {
			$select .= printLocation($line);
		}
		*/
		$select .= "	</td> \n";
		if($editCell) {
			$select .= "<td>";
			$select .="<input type=\"checkbox\" name=\"" . $line['id'] . "\" value=\"true\">";
		}
		$select .= "</tr> \n";
		//Clears all non nessassary info
		$tubeID = $line['id'];
		unset($line['Location']);
		unset($line['ContainerID']);
		unset($line['id']);
	}
	if (isEmpty($line)) {
			$select .= "<tr> \n <td> Empty <td> <td>";	
			return $select;
		}
	foreach ($line as $key_value => $col_value)  {
		if($formatCell) {
			if($loopCount % 2 == 0)
				$tempSelect .= "<tr> \n";
		}
		
		$tempSelect =makeEntry($linkArray, $key_value, $col_value, $oldKey_value, $oldCol_value, $tubeID, $search, $varname);
		
		if($formatCell) {
			if($loopCount % 2 == 0)
				$tempSelect .= "</tr> \n";
		}
		
		if($tempSelect) { //if it returns anything
			$loopCount++;
		}
		
		$select .= $tempSelect;
		$oldCol_value = $col_value;
		$oldKey_value = $key_value;
		
	}
	
	return $select;
}

/* $makeLink 
 * Website is the website to be directed to 
 * $key_value is the variable name to $_GET in the new 
 * website $varname is the WHILE condition in the new website.
 */
  
function makeEntry($linkArray, $key_value, $col_value, $oldKey_value, $oldCol_value, $tubeID, $search, $varname = 0) { 
	static $varname;
	static $makeLink;
	$searchString = $_SERVER['QUERY_STRING'];
	
	$searchString = urlencode($searchString);
	if ($makeLink) { 
		//WTF how does this work properly?		
		$select .= "<td> <font size=\"1.5\" color= \"3399CC\" > <a href=\"ModifyTube.php?id=" . $tubeID . "&$search\">$col_value</a>"; //This stuff is genis I can't get rid of delete the other link to retutn old functionality.'<a href=\"" . $varname . "?" . $oldKey_value . "=" .$oldCol_value . "&" . "search=" .$searchString . "\">" . $col_value . "</a> </font> </td>"; 
		$makeLink = FALSE;
	} elseif ($linkArray[$key_value] == "") {		
		//echo "Enters elseif </br> \n";
		$select .= "<font size=\"1.5\" color= \"3399CC\" > <td> $col_value </td> </font>\n";
	} else {
		//echo "Enters else </br> \n";
		$varname = $linkArray[$key_value]; //Varname used to be global now its not.  This is why there might be a bug.
		$makeLink = TRUE;
	}
		return $select;
}

/*
 * Takes a single line or a query to the containerID 
 * table and returns a concatanated version of that the 
 * row and col would look like on the box.
 */
?>
